<?php
namespace app\index\controller;

use app\index\model\User;
use think\Controller;
use think\Loader;
use think\Validate;

/**
 * 验证
 */
class Yz extends Controller
{

    /**
     * 验证器
     */

    // 独立验证
    public function index()
    {
        // 任何时候，都可以使用Validate类进行独立的验证操作
        $validate = new Validate([
            'name' => 'require|max:8',
            'email' => 'email',
        ]);
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@qq.com',
        ];
        $res = $validate->check($data);

        dump($res);

        if(!$res) print_r($validate->getError());
    }

    // 验证器
    public function a()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@qq.com',
        ];
        $validate = Loader::validate('User');
        $res = $validate->check($data);
        if(!$res) print_r($validate->getError());
    }

    // 使用助手函数实例化验证器
    public function b()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@qq.com',
        ];
        $validate = validate('User');
        $res = $validate->check($data);
        if(!$res) print_r($validate->getError());
    }


    /**
     * 验证规则
     */

    // 设置规则
    public function c()
    {
        // 可以在实例化Validate类的时候传入验证规则
        $rules = [
            'name' => 'require|max:8',
            'age' => 'number|between:1,120',
        ];
        $validate = new Validate($rules);

        // 也可以使用rule方法动态添加规则
        $validate->rule('zip', '/^\d{6}$/');
        $validate->rule([
            'email' => 'email',
        ]);
    }

    // 规则定义
    public function d()
    {
        $rules = [
            'name'  => 'require|max:25',
            'age'   => 'number|between:1,120',
        ];
        $validate = new Validate($rules);

        // 或者采用数组方式定义
        $rules = [
            'name' => ['require', 'max'=>8],
            'age' => ['number', 'between'=>'1,120'],
        ];
        $validate = new Validate($rules);
    }

    // 属性定义
    public function e()
    {
        $data = [
            'name' => 'thinkphp',
            'age' => 26,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = Loader::validate('User');
        $res = $validate->check($data);
        if(!$res) print_r($validate->getError());
    }

    // 验证数据
    public function f()
    {
        $rule = [
            'name' => 'require|max:8',
            'age' => 'number|between:1,120',
            'email' => 'email',
        ];
        $msg = [
            'name.require' => '名称必须填写',
            'name.max' => '名称最多不超过8个字符',
            'age.number' => '年龄必须是数字',
            'age.between' => '年龄只能在1~120之间',
            'email' => '邮箱格式不对',
        ];
        $data = [
            'name' => 'thinkphp',
            'age' => 18,
            'email' => 'thinkphp@qq.com',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);

        // 批量验证
        $result = $validate->batch()->check($data);
    }

    // 闭包函数验证
    public function g()
    {
        // 可以对某个字段使用闭包验证
        $validate = new Validate([
            'name' => function($value, $rule){
                return $rule==$value ? true : false;
            },
        ]);
    }

    // 自定义验证规则
    public function h()
    {
        $data = [
            'name' => 'thinkphpaa',
        ];
        $validate = Loader::validate('Us');
        $res = $validate->check($data);
        dump($res);
        if(!$res) print_r($validate->getError());
    }

    // 设置字段信息
    public function i()
    {
        $rule = [
            'name'  => 'require|max:25',
            'age'   => 'number|between:1,120',
            'email' => 'email',
        ];

        $field = [
            'name'  => '名称',
            'age'   => '年龄',
            'email' => '邮箱',
        ];

        $data = [
            'name'  => 'thinkphp',
            'age'   => 10,
            'email' => 'thinkphp@qq.com',
        ];

        $validate = new Validate($rule, [] , $field);
        $result   = $validate->check($data);
        dump($result);
        if(!$result) print_r($validate->getError());
    }


    /**
     * 错误信息
     */

    // 使用默认的错误提示信息
    public function j()
    {
        $rule = [
            'name'  => 'require|max:25',
//            'age'   => 'number|between:1,120',
            'age|年龄'   => 'number|between:1,120',
            'email' => 'email',
        ];

        $data = [
            'name'  => 'thinkphp',
            'age'   => 121,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = new Validate($rule);
        $result   = $validate->check($data);
        dump($result);
        if(!$result) echo $validate->getError();
    }

    // 验证规则和提示信息分开定义
    public function k()
    {
        $rule = [
            'name'  => 'require|max:25',
            'age'   => 'number|between:1,120',
            'email' => 'email',
        ];
        $msg = [
            'name.require' => '名称必须',
            'name.max'     => '名称最多不能超过25个字符',
            'age.number'   => '年龄必须是数字',
            'age.between'  => '年龄必须在1~120之间',
            'email'        => '邮箱格式错误',
        ];
        $data = [
            'name'  => 'thinkphp',
            'age'   => 121,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = new Validate($rule,$msg);
        $result   = $validate->check($data);
        dump($result);
        if(!$result) echo $validate->getError();
    }

    // 验证规则和提示信息一起定义
    public function l()
    {
        $rule = [
            ['name','require|max:25','名称必须|名称最多不能超过25个字符'],
            ['age','number|between:1,120','年龄必须是数字|年龄必须在1~120之间'],
            ['email','email','邮箱格式错误']
        ];

        $data = [
            'name'  => 'thinkphp',
            'age'   => 121,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = new Validate($rule);
        $result   = $validate->check($data);
        dump($result);
        if(!$result) echo $validate->getError();
    }


    /**
     * 验证场景
     */

    // 在定义验证规则的时候定义场景，并且验证不同场景的数据
    public function m()
    {
        $rule = [
            'name'  => 'require|max:25',
            'age'   => 'number|between:1,120',
            'email' => 'email',
        ];
        $msg = [
            'name.require' => '名称必须',
            'name.max'     => '名称最多不能超过25个字符',
            'age.number'   => '年龄必须是数字',
            'age.between'  => '年龄只能在1-120之间',
            'email'        => '邮箱格式错误',
        ];
        $data = [
            'name'  => 'thinkphp',
            'age'   => 10,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = new Validate($rule, $msg);
        $validate->scene('edit', ['name', 'age']);
        $result = $validate->scene('edit')->check($data);
        dump($result);
        if(!$result) echo $validate->getError();
    }


    /**
     * 控制器验证
     */

    // 在控制器中进行验证
    public function n()
    {
        $result = $this->validate(
            [
                'name' => 'thinkphp',
                'email' => 'thinkphp@126.com',
            ],
            [
                'name' => 'require|max:8',
                'email' => 'email',
            ]
        );

        if(true !== $result) print_r($result);
    }

    // 定义了验证类
    public function o()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@126.com',
        ];
        // 控制器中的验证代码可以简化为
        $result = $this->validate($data,'User');
        if(true !== $result) dump($result);

        // 如果要使用场景
        $result = $this->validate($data,'User.edit');
        if(true !== $result) dump($result);

        // 在validate方法中还支持做一些前置的操作回调
        $result = $this->validate($data,'User.edit',[],[$this,'some']);
        if(true !== $result) dump($result);
    }


    /**
     * 模型验证
     */

    // 模型验证代码可以简化为
    public function p()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@126.com',
        ];

        $User = new User();
        // 调用当前模型对应的User验证器类进行数据验证
        $result = $User->validate(true)->save($data);
        if(false === $result) dump($User->getError());
    }

    // 如果需要调用的验证器类和当前的模型名称不一致
    public function q()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@126.com',
        ];

        $User = new User;
        // 调用Member验证器类进行数据验证
        $result = $User->validate('Member')->save($data);
        if(false === $result){
            // 验证失败 输出错误信息
            dump($User->getError());
        }
    }

    // 支持场景验证
    public function r()
    {
        $data = [
            'name' => 'thinkphp',
            'email' => 'thinkphp@126.com',
        ];

        $User = new User;
        // 调用Member验证器类进行数据验证
        $result = $User->validate('User.edit')->save($data);
        if(false === $result){
            // 验证失败 输出错误信息
            dump($User->getError());
        }
    }


    /**
     * 内置规则
     */

    public function s()
    {

    }


    /**
     * 静态调用
     */

    // 如果需要使用内置的规则验证单个数据，可以使用静态调用的方式
    public function t()
    {
        // 日期格式验证
        Validate::dateFormat('2016-03-09','Y-m-d'); // true

        // 验证是否有效的日期
        Validate::is('2016-06-03','date'); // true

        // 验证是否有效邮箱地址
        $email = Validate::is('thinkphp@qq.com','email'); // true
        dump($email);

        // 验证是否在某个范围
        Validate::in('a',['a','b','c']); // true

        // 验证是否大于某个值
        Validate::gt(10,8); // true

        // 正则验证
        Validate::regex(100,'\d+'); // true
    }


    /**
     * 表单令牌
     */

    public function u()
    {

    }

}